package com.catsic.core.organ.dao;

import java.util.List;

import javax.persistence.QueryHint;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;

import com.catsic.base.BaseDao;
import com.catsic.core.entity.Organ;

/**
 * ClassName: OrganDao
 * Description: 机构DAO
 * date: 2014年2月27日 上午9:11:15
 * author: huyulei
 */
 
public interface OrganDao extends BaseDao<Organ, String>{
	
	/**
	 * 通过父节点查询所有子节点
	 * @param id
	 * @return
	 */
	@Query("select o from Organ o where o.parent.id = ?1 order by o.code asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Organ> findByPid(String id);
	
	/**
	 * 通过父节点code查询所有子节点
	 * @param id
	 * @return
	 */
	@Query("select o from Organ o where o.parent.code = ?1 order by o.code asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Organ> findByPcode(String code);
	
	/**
	 * 通过父节点查询所有子节点个数
	 * @param id
	 * @return
	 */
	@Query("select count(*) from Organ o where o.parent.id = ?1")
	public Integer findCountByPid(String id);
	
	
	
	
	/**
	 * findByCodeIsNotNullAndParentIdOrderByCodeDesc:查询父节点下所有编码不为空的子节点,按编码倒叙排列
	 * @author yangyudi
	 * @param id
	 * @return
	 */
	@Query("select o from Organ o where o.code is not null and o.parent.id = ?1 order by o.code desc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Organ> findByCodeIsNotNullAndParentCodeOrderByCodeDesc(String id);
	
	/**
	 * 通过机构code获取机构
	 * @param code
	 * @return
	 */
	@Query("select o from Organ o where o.code = ?1")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public Organ findByCode(String code);
	
	/**
	 * 查询机构编码为空的机构
	 * @param code
	 * @return
	 */
	public List<Organ> findByCodeIsNull();
	
	/**
	 * 通过级别与编码获取机构
	 * @param code
	 * @param jb
	 * @return
	 */
	public List<Organ> findByJbAndCodeOrderByCodeDesc(Integer jb,String code);

	/**获取机构
	 * @param id
	 * @return
	 */
	public Organ findByDescription(String id);
}
